package cn.stylefeng.guns.topspeedCarRace;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

public class ReadCarRace {
    private final static String token = "f6ccbdd12ecb428980fa24164f94a1681666856894192";
    private final static String ur2 = "https://cyoaliapi.rtidl.com/coron/api/ticketSourceResult/ticketSourceResultList.json";

    public static void main(String[] args) {
        JSONObject jsonObject = readRace();
        jisuan(jsonObject);
    }

    public static JSONObject readRace() {
        JSONObject params = new JSONObject();
        params.put("ticketId", 67);
        params.put("num", 1000);
        String data = HttpUtil.createPost(ur2)
                .header("token", token)
                .body(params.toJSONString())
                .execute().body();
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray array = jsonObject.getJSONArray("data");
        JSONObject jsonObject1 = array.getJSONObject(array.size() - 1);

        String path = StrUtil.format("D:\\乱七八糟\\快双\\\\{}{}.txt", "极速赛车", jsonObject1.getString("issue").substring(4));
        System.out.println("path:" + path);
        File file = new File(path);
        FileUtil.writeString(data, file, Charset.defaultCharset());
        return jsonObject;
    }

    public static void jisuan(JSONObject jsonObject) {
        JSONArray datas = jsonObject.getJSONArray("data");
        List<CarRace> races = Lists.newArrayList();
        for (int i = datas.size() - 1; i > 0; i--) {
            JSONObject data = datas.getJSONObject(i);
            String issue = data.getString("issue");
            String code = data.getString("code");
            String[] codes = code.split(" ");
            CarRace race = new CarRace(issue, parseInt(codes[0]), parseInt(codes[1]), parseInt(codes[2]),
                    parseInt(codes[3]), parseInt(codes[4]), parseInt(codes[5]),
                    parseInt(codes[6]), parseInt(codes[7]), parseInt(codes[8]),
                    parseInt(codes[9]), codes);
            races.add(race);
        }

        for (int i = 0; i < 10; i++) {
            print(races, i);
            System.out.println("<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>" + i);
        }
    }

    public static void print(List<CarRace> races, Integer num) {
        Map<Integer, Integer> map = initMap();
        for (int i = 0; i < races.size(); i++) {
            //最大的code
            Integer maxCode = sortByValue(map);

            CarRace race = races.get(i);
            Integer code = Integer.valueOf(race.getCodes()[num]);

            add(map, code);
            if (maxCode == code) {
                System.out.println(StrUtil.format("{}\t{}", race.subIssue(), code));
            }
        }
    }

    public static void add(Map<Integer, Integer> map, Integer code) {
        for (int i = 1; i <= 10; i++) {
            if (i == code) {
                map.put(i, 0);
            } else {
                Integer num = map.get(i);
                map.put(i, ++num);
            }
        }
    }

    public static Integer sortByValue(Map<Integer, Integer> map) {
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        //升序排序
        Collections.sort(list, Comparator.comparing(Map.Entry::getValue));
        return list.get(list.size() - 1).getKey();
    }

    public static Map<Integer, Integer> initMap() {
        Map<Integer, Integer> map = Maps.newTreeMap();
        map.put(1, 0);
        map.put(2, 1);
        map.put(3, 2);
        map.put(4, 3);
        map.put(5, 4);
        map.put(6, 5);
        map.put(7, 6);
        map.put(8, 7);
        map.put(9, 8);
        map.put(10, 9);
        return map;
    }

    public static Integer parseInt(String code) {
        return Integer.valueOf(code);
    }

    @Data
    public static class CarRace {
        private String issue;
        private Integer code1;
        private Integer code2;
        private Integer code3;
        private Integer code4;
        private Integer code5;
        private Integer code6;
        private Integer code7;
        private Integer code8;
        private Integer code9;
        private Integer code10;
        private String[] codes;

        public CarRace(String issue, Integer code1, Integer code2, Integer code3,
                       Integer code4, Integer code5, Integer code6, Integer code7,
                       Integer code8, Integer code9, Integer code10) {
            this.issue = issue;
            this.code1 = code1;
            this.code2 = code2;
            this.code3 = code3;
            this.code4 = code4;
            this.code5 = code5;
            this.code6 = code6;
            this.code7 = code7;
            this.code8 = code8;
            this.code9 = code9;
            this.code10 = code10;
        }

        public CarRace(String issue, Integer code1, Integer code2, Integer code3, Integer code4,
                       Integer code5, Integer code6, Integer code7, Integer code8, Integer code9,
                       Integer code10, String[] codes) {
            this.issue = issue;
            this.code1 = code1;
            this.code2 = code2;
            this.code3 = code3;
            this.code4 = code4;
            this.code5 = code5;
            this.code6 = code6;
            this.code7 = code7;
            this.code8 = code8;
            this.code9 = code9;
            this.code10 = code10;
            this.codes = codes;
        }

        public Integer subIssue() {
            return Integer.valueOf(this.issue.substring(9));
        }
    }
}
